/**
 * Copyright (c) 2006-2007 Jinyi.lu
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of GNU LGPL license by gun.org
 * which accompanies this distribution, and is available at
 * http://gnu.feld-it.at/copyleft/lesser.html
 * 
 * Project Homepage : http://code.google.com/p/demou
 * Source Code: svn checkout http://demou.googlecode.com/svn/trunk/ demou
 * Mailing list : jinyi.lu@gmail.com
 *                stonelu@126.com
 *
 *  
 * Contributors:
 *     JinYi.Lu - initial API and implementation
 */
package cn.tworen.demou.cmd;

import java.math.BigInteger;

/**
 * Created on  2007-1-26
 *
 * Title       : Header.java
 * Description : 
 * 
 * @author     : LuJinYi
 * @version    : 1.0
 * @Date       : 2007-1-26
 * History     : 
 * 
 */
public class Header {
	//amf stream id
	private int chunkIndex = 195;
	//amf source from
	private int[] source = new int[]{0,0,0,0};
	protected DataType dataType = null;
	private byte[] timer = new byte[]{0,0,0};
	private int headerLenght = 12;
	private byte headerFlag;
	private int bodyLenght = 0;
	public int getChunkIndex() {
		return this.chunkIndex;
	}

	public int[] getSource() {
		return this.source;
	}

	public void setChunkIndex(int index) {
		this.chunkIndex = index;
	}

	public void setSource(int[] source) {
		this.source = source;
	}
	public DataType getDataType() {
		return this.dataType;
	}

	public int getHeaderLenght() {
		return headerLenght;
	}

	public void setHeaderLenght(int headerLenght) {
		this.headerLenght = headerLenght;
	}
	/**
	 * @param dataType the dataType to set
	 */
	public void setDataType(DataType dataType) {
		this.dataType = dataType;
	}

	public int genHeaderFlag() {
		BigInteger bi = new BigInteger("" + this.chunkIndex, 10);
		if (this.headerLenght == 1) {
			BigInteger bi2 = new BigInteger("11000000", 2);
			bi = bi2.or(bi);
			return bi.intValue();
		} else if (this.headerLenght == 4){
			BigInteger bi2 = new BigInteger("10000000", 2);
			bi = bi2.or(bi);
			bi2 = new BigInteger("10111111", 2);
			bi = bi2.and(bi);
			return bi.intValue();
		} else if (this.headerLenght == 8){
			BigInteger bi2 = new BigInteger("01111111", 2);
			bi = bi2.and(bi);
			bi2 = new BigInteger("01000000", 2);
			bi = bi2.or(bi);
			return bi.intValue();
		} else if (this.headerLenght == 12){
			BigInteger bi2 = new BigInteger("00111111", 2);
			bi = bi2.and(bi);
			return bi.intValue();
		}
		return 0;
	}

	/**
	 * @hibernate.property
	 * @return Returns the timer.
	 */
	public byte[] getTimer() {
		return timer;
	}

	/**
	 * @param timer the timer to set
	 */
	public void setTimer(byte[] timer) {
		this.timer = timer;
	}
	public void setTimer(int value) {
		this.timer = new byte[3];
		this.timer[0] = (byte) (0xFF & (value >> 16));
		this.timer[1] = (byte) (0xFF & (value >> 8));
		this.timer[2] = (byte) (0xFF & (value >> 0));
	}

	/**
	 * @hibernate.property
	 * @return Returns the headerFlag.
	 */
	public byte getHeaderFlag() {
		return headerFlag;
	}

	/**
	 * @param headerFlag the headerFlag to set
	 */
	public void setHeaderFlag(byte headerFlag) {
		this.headerFlag = headerFlag;
	}

	/**
	 * @hibernate.property
	 * @return Returns the bodyLenght.
	 */
	public int getBodyLenght() {
		return bodyLenght;
	}

	/**
	 * @param bodyLenght the bodyLenght to set
	 */
	public void setBodyLenght(int bodyLenght) {
		this.bodyLenght = bodyLenght;
	}
	public Header clone() {
		Header newHeader = new Header();
		newHeader.setBodyLenght(bodyLenght);
		newHeader.setChunkIndex(this.chunkIndex);
		newHeader.setDataType(dataType);
		newHeader.setHeaderFlag(headerFlag);
		newHeader.setHeaderLenght(headerLenght);
		newHeader.setSource(source);
		newHeader.setTimer(timer);
		return newHeader;
	}
}
